/**
 * @className: OvertimeService
 * @author: Csh
 * @date: 2024/4/1 21:17
 * @Version: 1.0
 * @description:
 */
package com.zhuojy.equity.mall.admin.service.AttendanceServices;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.AttendanceGroup;
import com.zhuojy.equity.mall.admin.common.entity.bo.DatesBO;
import com.zhuojy.equity.mall.admin.common.entity.bo.OvertimeBO;
import com.zhuojy.equity.mall.admin.common.entity.po.DatesPO;
import com.zhuojy.equity.mall.admin.common.entity.po.OvertimePO;
import com.zhuojy.equity.mall.admin.common.pojo.JsonResult;
import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.AttendanceGroupMapper;

import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.DatesMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.OvertimeMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor
public class OvertimeService extends ServiceImpl<OvertimeMapper, OvertimePO> {

    @Autowired
    private DatesMapper datesMapper;
    @Autowired
    private AttendanceGroupMapper attendanceGroupMapper;


    public int addOrUpOvertime(OvertimeBO overtimeBO) {
        System.out.println(overtimeBO);
        ArrayList<Integer> list = new ArrayList<>();
        Integer result = null;
        if(overtimeBO.getId()==null){

            for (DatesBO datesBO : overtimeBO.getDatesBOS()) {
                DatesPO datesPO = null;
                if(datesBO!=null){
                    datesPO = new DatesPO(null, datesBO.getOvertimeStatic(), datesBO.getOvertimeManner(), datesBO.getStartTime(), datesBO.getMinTime(), datesBO.getBreaks(), datesBO.getOvertimeTime(), datesBO.getOvertimeSettlement(), datesBO.getCompensatoryLeave(), datesBO.getApproval(), datesBO.getOvertimeClock());
                    datesMapper.insert(datesPO);
                    list.add(datesPO.getId());
                }else{
                    list.add(null);
                }

            }
            result = this.baseMapper.insert(new OvertimePO(null, overtimeBO.getOvertimeName(), overtimeBO.getOvertimeApplyId(), overtimeBO.getOvertimeHead(), overtimeBO.getOvertimeTimeUnit(), overtimeBO.getOvertimeRound(), overtimeBO.getOvertimeConverted(), overtimeBO.getOvertimeMax(), list.get(0), list.get(1), list.get(2), new Date(), overtimeBO.getCreatedBy(), null, null, null, null, null, null));

        }else {
            for (DatesBO datesBO : overtimeBO.getDatesBOS()) {
                DatesPO datesPO = null;
                if(datesBO!=null){
                    datesMapper.deleteById(datesBO.getId());
                    datesPO = new DatesPO(null, datesBO.getOvertimeStatic(), datesBO.getOvertimeManner(), datesBO.getStartTime(), datesBO.getMinTime(), datesBO.getBreaks(), datesBO.getOvertimeTime(), datesBO.getOvertimeSettlement(), datesBO.getCompensatoryLeave(), datesBO.getApproval(), datesBO.getOvertimeClock());
                    datesMapper.insert(datesPO);
                    list.add(datesPO.getId());
                }else{
                    list.add(null);
                }

            }
            result = this.baseMapper.updateById(new OvertimePO(overtimeBO.getId(), overtimeBO.getOvertimeName(), overtimeBO.getOvertimeApplyId(), overtimeBO.getOvertimeHead(), overtimeBO.getOvertimeTimeUnit(), overtimeBO.getOvertimeRound(), overtimeBO.getOvertimeConverted(), overtimeBO.getOvertimeMax(), list.get(0), list.get(1), list.get(2), null, null, new Date(), overtimeBO.getCreatedBy(), null, null, null, null));

        }
        return result;
    }

    public JsonResult findAllOvertime(OvertimeBO overtimeBO, Integer pageNum, Integer pageSize) {
        IPage page = new Page(pageNum, pageSize);
        IPage<OvertimePO> iPage = this.baseMapper.selectPage(page, new LambdaQueryWrapper<OvertimePO>().like(OvertimePO::getOvertimeName, overtimeBO.getOvertimeName()));
        for (OvertimePO overtimePO : iPage.getRecords()) {
            overtimePO.setWeek(datesMapper.selectOne(new LambdaQueryWrapper<DatesPO>().eq(DatesPO::getId,overtimePO.getWeekday())));
            overtimePO.setRest(datesMapper.selectOne(new LambdaQueryWrapper<DatesPO>().eq(DatesPO::getId,overtimePO.getRestdays())));
            overtimePO.setHoli(datesMapper.selectOne(new LambdaQueryWrapper<DatesPO>().eq(DatesPO::getId,overtimePO.getHolidays())));
            if(overtimePO.getOvertimeApplyId()!=0){
                overtimePO.setOvertimeApplyName(attendanceGroupMapper.selectOne(new LambdaQueryWrapper<AttendanceGroup>().eq(AttendanceGroup::getId,overtimePO.getOvertimeApplyId())).getAttendanceName());
            }else{
                overtimePO.setOvertimeApplyName("全公司");
            }
        }



        return JsonResult.success("成功",iPage.getRecords(),iPage.getTotal());
    }

    public JsonResult findOvertimeOne(Integer id) {
        OvertimePO overtimePO = this.baseMapper.selectOne(new LambdaQueryWrapper<OvertimePO>().eq(OvertimePO::getId, id));
        overtimePO.setWeek(datesMapper.selectOne(new LambdaQueryWrapper<DatesPO>().eq(DatesPO::getId,overtimePO.getWeekday())));
        overtimePO.setRest(datesMapper.selectOne(new LambdaQueryWrapper<DatesPO>().eq(DatesPO::getId,overtimePO.getRestdays())));
        overtimePO.setHoli(datesMapper.selectOne(new LambdaQueryWrapper<DatesPO>().eq(DatesPO::getId,overtimePO.getHolidays())));
        return JsonResult.success("成功",overtimePO);
    }

    public int delOvertime(Integer id) {
        return this.baseMapper.deleteById(id);
    }
}
